package com.mpyy.handler.sales;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mpyy.pojo.*;
import com.mpyy.service.sales.ProSalesServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

/**
 * @Description ProSalesHandler
 * @Author qzy
 * @Date 2022-03-07 8:24
 * @Version 1.0
 */
@Controller
public class ProSalesHandler {
    @Autowired
    @Qualifier(value = "proSalesServiceImpl")
    private ProSalesServiceInter proSalesServiceInter;

    /**
     * 查询库存明细表，获取商品信息，填充到商品清单的tab栏
     */
    @RequestMapping("/showProSalesBill/{inveDescDepot}")
    @ResponseBody
    public String showProSalesBill(@PathVariable String inveDescDepot , Paging paging){
        //获取查询显示的商品信息
        List<InventoryDesc> inventoryDescs = proSalesServiceInter.searchProBill(inveDescDepot,paging);

        //创建一个Map,拼接JSON格式传给页面
        HashMap<Object, Object> proSalesBillJSON = new HashMap<>();
        proSalesBillJSON.put("code",0);
        proSalesBillJSON.put("msg","");
        proSalesBillJSON.put("count",proSalesServiceInter.searchProBillsCount());
        proSalesBillJSON.put("data",inventoryDescs);
        //System.out.println("JSON格式的字符串对象:" + JSON.toJSONString(proSalesBillJSON));
        return JSON.toJSONString(proSalesBillJSON);
    }

    /**
     * 根据商品编号查询该商品批次信息进行显示
     */
    @RequestMapping("/showProBatchNum/{proId}")
    @ResponseBody
    public String showProBatchNum(@PathVariable String proId){
        InventoryDesc inventoryDesc = new InventoryDesc();
        inventoryDesc.setProId(proId);
        //查询商品批次信息
        List<InventoryDesc> inventoryDescList = proSalesServiceInter.searchProBatchNumInfo(inventoryDesc);

        //创建一个Map,拼接JSON格式传给页面
        HashMap<Object, Object> inventoryDescJSON = new HashMap<>();
        inventoryDescJSON.put("code",0);
        inventoryDescJSON.put("msg","");
        inventoryDescJSON.put("data",inventoryDescList);
        //System.out.println("JSON格式的字符串对象:" + JSON.toJSONString(inventoryDescJSON));
        return JSON.toJSONString(inventoryDescJSON);
    }

    /**
     * 新增商品销售信息
     * @param proSalesOrder JSON格式的商品销售信息对象
     * @param proSalesOrderDesc JSON格式的商品销售明细集合
     * @return 是否新增成功
     * @throws Exception 异常
     */
    @RequestMapping("/insertSalesProInfo/{proSalesOrder}/{proSalesOrderDesc}")
    @ResponseBody
    public String insertSalesProInfo(@PathVariable String proSalesOrder, @PathVariable String proSalesOrderDesc) throws Exception {
        //实例化一个销售订单对象
        SaleOrder saleOrder = new SaleOrder();
        //获取页面传递过来的销售订单数据，并转成pojo对象
        JSONObject o = JSONObject.parseObject(proSalesOrder);
        //将值赋给对应的属性
        saleOrder.setSalOrdId(o.getString("salOrdId"));
        saleOrder.setEmpId(o.getString("empId"));
        saleOrder.setDepotId(o.getString("depotId"));
        saleOrder.setCustId(o.getString("custId"));
        saleOrder.setSalOrdDate(o.getDate("salOrdDate"));
        saleOrder.setSalOrdStatus(o.getString("salOrdStatus"));
        saleOrder.setSalOrdPaidIn(o.getDouble("salOrdPaidIn"));
        saleOrder.setSalOrdNum(o.getInteger("salOrdNum"));
        saleOrder.setSalOrdOriginalNum(o.getString("salOrdOriginalNum"));
        saleOrder.setSalOrdComment(o.getString("salOrdComment"));
        saleOrder.setSalOrdReceivable(o.getDouble("salOrdReceivable"));

        //实例化一个销售订单明细对象
        SaleOrderDesc saleOrderDesc = new SaleOrderDesc();
        //创建一个StringBuffer对象，获取页面传递过来的销售订单明细数据，拼接还原后加入StringBuffer中
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[" + proSalesOrderDesc + "]");

        /*List<Map<String , Object>> mapList = (List<Map<String, Object>>) JSONArray.parse(stringBuffer.toString());
        for (Map<String, Object> stringObjectMap : mapList) {
            for (String s : stringObjectMap.keySet()) {
                System.out.println(s);
            }
        }*/
        //调用自定义的转换工具类
        List<SaleOrderDesc> saleOrderDescs = ProSalesUtil.<SaleOrderDesc>decomposeJsonArray(stringBuffer, SaleOrderDesc.class);

        //调用新增商品销售信息的方法
        boolean b = proSalesServiceInter.insertProSalesInfo(saleOrder, saleOrderDescs);
        return JSON.toJSONString(b);
    }

    /**
     * 查询客户信息,显示到客户姓名的下拉列表
     */
    @RequestMapping("/showCustomerInfo")
    @ResponseBody
    public String showCustomerInfo(){
        //调用查询客户信息的方法
        List<CustomerInfo> customerInfos = proSalesServiceInter.searchCustomerInfo();
        //创建一个Map,拼接JSON格式传给页面
        HashMap<Object, Object> customerInfosJSON = new HashMap<>();
        customerInfosJSON.put("code",0);
        customerInfosJSON.put("msg","");
        customerInfosJSON.put("data",customerInfos);
        //System.out.println("JSON格式的字符串对象:" + JSON.toJSONString(customerInfosJSON));
        return JSON.toJSONString(customerInfosJSON);
    }

    /**
     * 查询仓库信息,显示到出货仓库的下拉列表
     */
    @RequestMapping("/showDepotInfo")
    @ResponseBody
    public String showDepotInfo(){
        List<DepotInfo> depotInfos = proSalesServiceInter.searchDepotInfo();
        return JSON.toJSONString(depotInfos);
    }

    /**
     * 查询员工信息，,显示到经办人的下拉列表
     */
    @RequestMapping("/showEmpInfo")
    @ResponseBody
    public String showEmpInfo(){
        List<EmployeeInfo> employeeInfos = proSalesServiceInter.searchEmpInfo();
        return JSON.toJSONString(employeeInfos);
    }

    /**
     * 查询客户类型信息
     */
    @RequestMapping("/showCustomerTypeInfo")
    @ResponseBody
    public String showCustomerTypeInfo(){
        List<CustomerType> customerTypes = proSalesServiceInter.searchCustomerTypeInfo();
        return JSON.toJSONString(customerTypes);
    }

    /**
     * 修改客户信息
     */
    @RequestMapping("/updateAlertCustomerInfo")
    @ResponseBody
    public String updateAlertCustomerInfo(CustomerInfo customerInfo){
        //调用修改方法
        int rows = proSalesServiceInter.updateAlertCustomerInfo(customerInfo);
        return JSON.toJSONString(rows);
    }

    /**
     * 新增客户信息
     */
    @RequestMapping("/updateInsertCustomerInfo")
    @ResponseBody
    public String updateInsertCustomerInfo(CustomerInfo customerInfo){
        //调用新增方法
        int rows = proSalesServiceInter.updateInsertCustomerInfo(customerInfo);
        return JSON.toJSONString(rows);
    }

    /**
     * 带条件查找商品清单信息,显示
     */
    @RequestMapping("/showProBillInfo/{inveDescDepot}")
    @ResponseBody
    public String showProBillInfo(@PathVariable String inveDescDepot, String proIdOrName){
        //创建一个库存明细对象
        InventoryDesc inventoryDesc = new InventoryDesc();
        inventoryDesc.setInveDescDepot(inveDescDepot);
        inventoryDesc.setProId(proIdOrName);

        //调用查询的方法
        List<InventoryDesc> inventoryDescs = proSalesServiceInter.searchProBillInfo(inventoryDesc);

        //创建一个Map,拼接JSON格式传给页面
        HashMap<Object, Object> proSalesBillJSON = new HashMap<>();
        proSalesBillJSON.put("code",0);
        proSalesBillJSON.put("msg","");
        proSalesBillJSON.put("data",inventoryDescs);
        //System.out.println("JSON格式的字符串对象:" + JSON.toJSONString(proSalesBillJSON));
        return JSON.toJSONString(proSalesBillJSON);
    }

    /**
     * 查询最近进货的商品信息（近一个月内）,显示
     */
    @RequestMapping("/showRecentPurchaseInfo")
    @ResponseBody
    public String showRecentPurchaseInfo(){
        //调用查询的方法
        List<InventoryDesc> inventoryDescs = proSalesServiceInter.searchRecentPurchaseInfo();

        //创建一个Map,拼接JSON格式传给页面
        HashMap<Object, Object> recentPurInfoJSON = new HashMap<>();
        recentPurInfoJSON.put("code",0);
        recentPurInfoJSON.put("msg","");
        recentPurInfoJSON.put("data",inventoryDescs);
        //System.out.println("JSON格式的字符串对象:" + JSON.toJSONString(recentPurInfoJSON));
        return JSON.toJSONString(recentPurInfoJSON);
    }

    /**
     * 查询商品级别表所有数据，显示在树形控件
     */
    @RequestMapping(value = "/showProductTypeInfo", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String showProductTypeInfo(){
        //调用查询的方法
        List<ProductType> productTypes = proSalesServiceInter.searchProductTypeInfo();

        //转成tree支持的JSON格式返回
        String treeData = JSON.toJSONString(productTypes).replaceAll("proTypeId","id").replaceAll("proLevelParId","parentId").replaceAll("proTypeName","title");
        //System.out.println(treeData);
        return treeData;
    }

    /**
     * 查询商品列表的信息【所在仓库 | 商品类别编号（商品编号）】
     */
    @RequestMapping(value = "/showProListInfo/{proTypeId}")
    @ResponseBody
    public String showProListInfo(@PathVariable String proTypeId){
        //调用查询的方法
        List<InventoryDesc> inventoryDescs = proSalesServiceInter.searchProListTree(proTypeId);

        //创建一个Map,拼接JSON格式传给页面
        HashMap<Object, Object> ProListInfoJSON = new HashMap<>();
        ProListInfoJSON.put("code",0);
        ProListInfoJSON.put("msg","");
        ProListInfoJSON.put("data",inventoryDescs);
        //System.out.println("JSON格式的字符串对象:" + JSON.toJSONString(ProListInfoJSON));
        return JSON.toJSONString(ProListInfoJSON);
    }

    /**
     * 查询商品销售历史信息
     */
    @RequestMapping(value = {"/showProSaleHisInfo/{proId}"})
    @ResponseBody
    public String showProSaleHisInfo(@PathVariable String proId){
        //创建销售
        SaleOrderDesc saleOrderDesc = new SaleOrderDesc();
        saleOrderDesc.setProductId(proId);
        //调用查询的方法
        List<SaleOrderDesc> saleOrderDescs = proSalesServiceInter.searchProSaleHisInfo(saleOrderDesc);

        //创建一个Map,拼接JSON格式传给页面
        HashMap<Object, Object> ProSaleHisInfoJSON = new HashMap<>();
        ProSaleHisInfoJSON.put("code",0);
        ProSaleHisInfoJSON.put("msg","");
        ProSaleHisInfoJSON.put("data",saleOrderDescs);
        //System.out.println("JSON格式的字符串对象:" + JSON.toJSONString(ProSaleHisInfoJSON));
        return JSON.toJSONString(ProSaleHisInfoJSON);
    }
}


//    Application Server was not connected before run configuration stop, reason: Unable to ping server at localhost:1099
//    在运行配置停止之前没有连接应用程序服务器，原因：无法在本地主机上的ping服务器：1099